44dd227381c2037dbc64b069bcb9adb87f24793a
[lhc/web/wiklou.git] / includes / specials / SpecialWhatlinkshere.php
1 <?php
2 /**
3 * Implements Special:Whatlinkshere
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @todo Use some variant of Pager or something; the pagination here is lousy.
22 */
23
24 /**
25 * Implements Special:Whatlinkshere
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialWhatLinksHere extends SpecialPage {
30
31 // Stored objects
32 protected $opts, $target, $selfTitle;
33
34 protected $limits = array( 20, 50, 100, 250, 500 );
35
36 public function __construct() {
37 parent::__construct( 'Whatlinkshere' );
38 }
39
40 function execute( $par ) {
41 $out = $this->getOutput();
42
43 $this->setHeaders();
44
45 $opts = new FormOptions();
46
47 $opts->add( 'target', '' );
48 $opts->add( 'namespace', '', FormOptions::INTNULL );
49 $opts->add( 'limit', 50 );
50 $opts->add( 'from', 0 );
51 $opts->add( 'back', 0 );
52 $opts->add( 'hideredirs', false );
53 $opts->add( 'hidetrans', false );
54 $opts->add( 'hidelinks', false );
55 $opts->add( 'hideimages', false );
56
57 $opts->fetchValuesFromRequest( $this->getRequest() );
58 $opts->validateIntBounds( 'limit', 0, 5000 );
59
60 // Give precedence to subpage syntax
61 if ( isset($par) ) {
62 $opts->setValue( 'target', $par );
63 }
64
65 // Bind to member variable
66 $this->opts = $opts;
67
68 $this->target = Title::newFromURL( $opts->getValue( 'target' ) );
69 if( !$this->target ) {
70 $out->addHTML( $this->whatlinkshereForm() );
71 return;
72 }
73
74 $this->getSkin()->setRelevantTitle( $this->target );
75
76
77 $this->selfTitle = $this->getTitle( $this->target->getPrefixedDBkey() );
78
79 $out->setPageTitle( wfMsg( 'whatlinkshere-title', $this->target->getPrefixedText() ) );
80 $out->setSubtitle( wfMsg( 'whatlinkshere-backlink', Linker::link( $this->target, $this->target->getPrefixedText(), array(), array( 'redirect' => 'no' ) ) ) );
81
82 $this->showIndirectLinks( 0, $this->target, $opts->getValue( 'limit' ),
83 $opts->getValue( 'from' ), $opts->getValue( 'back' ) );
84 }
85
86 /**
87 * @param $level int Recursion level
88 * @param $target Title Target title
89 * @param $limit int Number of entries to display
90 * @param $from Title Display from this article ID
91 * @param $back Title Display from this article ID at backwards scrolling
92 * @private
93 */
94 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
95 global $wgMaxRedirectLinksRetrieved;
96 $out = $this->getOutput();
97 $dbr = wfGetDB( DB_SLAVE );
98 $options = array();
99
100 $hidelinks = $this->opts->getValue( 'hidelinks' );
101 $hideredirs = $this->opts->getValue( 'hideredirs' );
102 $hidetrans = $this->opts->getValue( 'hidetrans' );
103 $hideimages = $target->getNamespace() != NS_FILE || $this->opts->getValue( 'hideimages' );
104
105 $fetchlinks = (!$hidelinks || !$hideredirs);
106
107 // Make the query
108 $plConds = array(
109 'page_id=pl_from',
110 'pl_namespace' => $target->getNamespace(),
111 'pl_title' => $target->getDBkey(),
112 );
113 if( $hideredirs ) {
114 $plConds['page_is_redirect'] = 0;
115 } elseif( $hidelinks ) {
116 $plConds['page_is_redirect'] = 1;
117 }
118
119 $tlConds = array(
120 'page_id=tl_from',
121 'tl_namespace' => $target->getNamespace(),
122 'tl_title' => $target->getDBkey(),
123 );
124
125 $ilConds = array(
126 'page_id=il_from',
127 'il_to' => $target->getDBkey(),
128 );
129
130 $namespace = $this->opts->getValue( 'namespace' );
131 if ( is_int($namespace) ) {
132 $plConds['page_namespace'] = $namespace;
133 $tlConds['page_namespace'] = $namespace;
134 $ilConds['page_namespace'] = $namespace;
135 }
136
137 if ( $from ) {
138 $tlConds[] = "tl_from >= $from";
139 $plConds[] = "pl_from >= $from";
140 $ilConds[] = "il_from >= $from";
141 }
142
143 // Read an extra row as an at-end check
144 $queryLimit = $limit + 1;
145
146 // Enforce join order, sometimes namespace selector may
147 // trigger filesorts which are far less efficient than scanning many entries
148 $options[] = 'STRAIGHT_JOIN';
149
150 $options['LIMIT'] = $queryLimit;
151 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
152
153 if( $fetchlinks ) {
154 $options['ORDER BY'] = 'pl_from';
155 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
156 $plConds, __METHOD__, $options );
157 }
158
159 if( !$hidetrans ) {
160 $options['ORDER BY'] = 'tl_from';
161 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
162 $tlConds, __METHOD__, $options );
163 }
164
165 if( !$hideimages ) {
166 $options['ORDER BY'] = 'il_from';
167 $ilRes = $dbr->select( array( 'imagelinks', 'page' ), $fields,
168 $ilConds, __METHOD__, $options );
169 }
170
171 if( ( !$fetchlinks || !$dbr->numRows($plRes) ) && ( $hidetrans || !$dbr->numRows($tlRes) ) && ( $hideimages || !$dbr->numRows($ilRes) ) ) {
172 if ( 0 == $level ) {
173 $out->addHTML( $this->whatlinkshereForm() );
174
175 // Show filters only if there are links
176 if( $hidelinks || $hidetrans || $hideredirs || $hideimages )
177 $out->addHTML( $this->getFilterPanel() );
178
179 $errMsg = is_int($namespace) ? 'nolinkshere-ns' : 'nolinkshere';
180 $out->addWikiMsg( $errMsg, $this->target->getPrefixedText() );
181 }
182 return;
183 }
184
185 // Read the rows into an array and remove duplicates
186 // templatelinks comes second so that the templatelinks row overwrites the
187 // pagelinks row, so we get (inclusion) rather than nothing
188 if( $fetchlinks ) {
189 foreach ( $plRes as $row ) {
190 $row->is_template = 0;
191 $row->is_image = 0;
192 $rows[$row->page_id] = $row;
193 }
194 }
195 if( !$hidetrans ) {
196 foreach ( $tlRes as $row ) {
197 $row->is_template = 1;
198 $row->is_image = 0;
199 $rows[$row->page_id] = $row;
200 }
201 }
202 if( !$hideimages ) {
203 foreach ( $ilRes as $row ) {
204 $row->is_template = 0;
205 $row->is_image = 1;
206 $rows[$row->page_id] = $row;
207 }
208 }
209
210 // Sort by key and then change the keys to 0-based indices
211 ksort( $rows );
212 $rows = array_values( $rows );
213
214 $numRows = count( $rows );
215
216 // Work out the start and end IDs, for prev/next links
217 if ( $numRows > $limit ) {
218 // More rows available after these ones
219 // Get the ID from the last row in the result set
220 $nextId = $rows[$limit]->page_id;
221 // Remove undisplayed rows
222 $rows = array_slice( $rows, 0, $limit );
223 } else {
224 // No more rows after
225 $nextId = false;
226 }
227 $prevId = $from;
228
229 if ( $level == 0 ) {
230 $out->addHTML( $this->whatlinkshereForm() );
231 $out->addHTML( $this->getFilterPanel() );
232 $out->addWikiMsg( 'linkshere', $this->target->getPrefixedText() );
233
234 $prevnext = $this->getPrevNext( $prevId, $nextId );
235 $out->addHTML( $prevnext );
236 }
237
238 $out->addHTML( $this->listStart( $level ) );
239 foreach ( $rows as $row ) {
240 $nt = Title::makeTitle( $row->page_namespace, $row->page_title );
241
242 if ( $row->page_is_redirect && $level < 2 ) {
243 $out->addHTML( $this->listItem( $row, $nt, true ) );
244 $this->showIndirectLinks( $level + 1, $nt, $wgMaxRedirectLinksRetrieved );
245 $out->addHTML( Xml::closeElement( 'li' ) );
246 } else {
247 $out->addHTML( $this->listItem( $row, $nt ) );
248 }
249 }
250
251 $out->addHTML( $this->listEnd() );
252
253 if( $level == 0 ) {
254 $out->addHTML( $prevnext );
255 }
256 }
257
258 protected function listStart( $level ) {
259 return Xml::openElement( 'ul', ( $level ? array() : array( 'id' => 'mw-whatlinkshere-list' ) ) );
260 }
261
262 protected function listItem( $row, $nt, $notClose = false ) {
263 # local message cache
264 static $msgcache = null;
265 if ( $msgcache === null ) {
266 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
267 'whatlinkshere-links', 'isimage' );
268 $msgcache = array();
269 foreach ( $msgs as $msg ) {
270 $msgcache[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
271 }
272 }
273
274 if( $row->page_is_redirect ) {
275 $query = array( 'redirect' => 'no' );
276 } else {
277 $query = array();
278 }
279
280 $link = Linker::linkKnown(
281 $nt,
282 null,
283 array(),
284 $query
285 );
286
287 // Display properties (redirect or template)
288 $propsText = '';
289 $props = array();
290 if ( $row->page_is_redirect )
291 $props[] = $msgcache['isredirect'];
292 if ( $row->is_template )
293 $props[] = $msgcache['istemplate'];
294 if( $row->is_image )
295 $props[] = $msgcache['isimage'];
296
297 if ( count( $props ) ) {
298 $propsText = '(' . implode( $msgcache['semicolon-separator'], $props ) . ')';
299 }
300
301 # Space for utilities links, with a what-links-here link provided
302 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
303 $wlh = Xml::wrapClass( "($wlhLink)", 'mw-whatlinkshere-tools' );
304
305 return $notClose ?
306 Xml::openElement( 'li' ) . "$link $propsText $wlh\n" :
307 Xml::tags( 'li', null, "$link $propsText $wlh" ) . "\n";
308 }
309
310 protected function listEnd() {
311 return Xml::closeElement( 'ul' );
312 }
313
314 protected function wlhLink( Title $target, $text ) {
315 static $title = null;
316 if ( $title === null )
317 $title = $this->getTitle();
318
319 return Linker::linkKnown(
320 $title,
321 $text,
322 array(),
323 array( 'target' => $target->getPrefixedText() )
324 );
325 }
326
327 function makeSelfLink( $text, $query ) {
328 return Linker::linkKnown(
329 $this->selfTitle,
330 $text,
331 array(),
332 $query
333 );
334 }
335
336 function getPrevNext( $prevId, $nextId ) {
337 global $wgLang;
338 $currentLimit = $this->opts->getValue( 'limit' );
339 $fmtLimit = $wgLang->formatNum( $currentLimit );
340 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
341 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
342
343 $changed = $this->opts->getChangedValues();
344 unset($changed['target']); // Already in the request title
345
346 if ( 0 != $prevId ) {
347 $overrides = array( 'from' => $this->opts->getValue( 'back' ) );
348 $prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
349 }
350 if ( 0 != $nextId ) {
351 $overrides = array( 'from' => $nextId, 'back' => $prevId );
352 $next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
353 }
354
355 $limitLinks = array();
356 foreach ( $this->limits as $limit ) {
357 $prettyLimit = $wgLang->formatNum( $limit );
358 $overrides = array( 'limit' => $limit );
359 $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $changed, $overrides ) );
360 }
361
362 $nums = $wgLang->pipeList( $limitLinks );
363
364 return wfMsgHtml( 'viewprevnext', $prev, $next, $nums );
365 }
366
367 function whatlinkshereForm() {
368 global $wgScript;
369
370 // We get nicer value from the title object
371 $this->opts->consumeValue( 'target' );
372 // Reset these for new requests
373 $this->opts->consumeValues( array( 'back', 'from' ) );
374
375 $target = $this->target ? $this->target->getPrefixedText() : '';
376 $namespace = $this->opts->consumeValue( 'namespace' );
377
378 # Build up the form
379 $f = Xml::openElement( 'form', array( 'action' => $wgScript ) );
380
381 # Values that should not be forgotten
382 $f .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() );
383 foreach ( $this->opts->getUnconsumedValues() as $name => $value ) {
384 $f .= Html::hidden( $name, $value );
385 }
386
387 $f .= Xml::fieldset( wfMsg( 'whatlinkshere' ) );
388
389 # Target input
390 $f .= Xml::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target',
391 'mw-whatlinkshere-target', 40, $target );
392
393 $f .= ' ';
394
395 # Namespace selector
396 $f .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . '&#160;' .
397 Xml::namespaceSelector( $namespace, '' );
398
399 $f .= ' ';
400
401 # Submit
402 $f .= Xml::submitButton( wfMsg( 'allpagessubmit' ) );
403
404 # Close
405 $f .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' ) . "\n";
406
407 return $f;
408 }
409
410 /**
411 * Create filter panel
412 *
413 * @return string HTML fieldset and filter panel with the show/hide links
414 */
415 function getFilterPanel() {
416 global $wgLang;
417 $show = wfMsgHtml( 'show' );
418 $hide = wfMsgHtml( 'hide' );
419
420 $changed = $this->opts->getChangedValues();
421 unset($changed['target']); // Already in the request title
422
423 $links = array();
424 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
425 if( $this->target->getNamespace() == NS_FILE )
426 $types[] = 'hideimages';
427
428 // Combined message keys: 'whatlinkshere-hideredirs', 'whatlinkshere-hidetrans', 'whatlinkshere-hidelinks', 'whatlinkshere-hideimages'
429 // To be sure they will be find by grep
430 foreach( $types as $type ) {
431 $chosen = $this->opts->getValue( $type );
432 $msg = $chosen ? $show : $hide;
433 $overrides = array( $type => !$chosen );
434 $links[] = wfMsgHtml( "whatlinkshere-{$type}", $this->makeSelfLink( $msg, array_merge( $changed, $overrides ) ) );
435 }
436 return Xml::fieldset( wfMsg( 'whatlinkshere-filters' ), $wgLang->pipeList( $links ) );
437 }
438 }